n = int(input())
a = 0
for i in range(n):
    t = 0
    jiecheng = int(input())
    b = int(input())
    for i in range(1,(jiecheng+1)):
        a *= i
        if i == b:
            t += 1
            if a == b:
                t += 1
        else:
            if a == b:
                t += 1
    print(t)

# 先在阶乘的计算过程中找到输入的数出现几次,然后只需要将阶乘过程中出现的数字都判断一遍即可